package com.metro.paperless.common.utils;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class SendMail {
	private JavaMailSenderImpl mailSender;
	private SimpleMailMessage simpleMailMessage;

	/**
	 * @方法名: sendMail
	 * @参数名：@param subject 邮件主题
	 * @参数名：@param content 邮件主题内容
	 * @参数名：@param to 收件人Email地址
	 * @描述语: 发送邮件
	 */
	public void sendMail(String subject, String content, String to) {
		try {
			simpleMailMessage.setSubject(subject); // 设置邮件主题
			simpleMailMessage.setTo(to); // 设定收件人
			simpleMailMessage.setText(content); // 设置邮件主题内容
			mailSender.send(simpleMailMessage); // 发送邮件
			System.out.println("发送给：" + to + "的邮件发送成功...");
		}catch (Exception e){
			System.out.println("邮件发送失败："+e.getMessage());
		}
	}
	/**
	 * 发送html格式的邮件
	 * @param subject
	 * @param content
	 * @param to
	 * @author xk
	 */
	public void sendHtmlMail(String subject, String content, String to) {
		MimeMessage mailMessage = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper;
		try {
			messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
			messageHelper.setFrom(mailSender.getUsername());
			messageHelper.setTo(to);
			messageHelper.setSubject(subject);
			// true 表示启动HTML格式的邮件
			messageHelper.setText(content, true);
			mailSender.send(mailMessage);
			System.out.println("发送给：" + to + "的邮件发送成功...");
		}catch (Exception e){
			System.out.println("邮件发送失败："+e.getMessage());
		}
		
	}

	// Spring 依赖注入
	public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
		this.simpleMailMessage = simpleMailMessage;
	}

	// Spring 依赖注入
	public void setMailSender(JavaMailSenderImpl mailSender) {
		this.mailSender = mailSender;
	}

 

}
